package cn.hahah.module.blog.service.userlike;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userlike.vo.*;
import cn.hahah.module.blog.dal.dataobject.userlike.UserLikeDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userlike.UserLikeMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户点赞 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserLikeServiceImpl implements UserLikeService {

    @Resource
    private UserLikeMapper userLikeMapper;

    @Override
    public Long createUserLike(UserLikeSaveReqVO createReqVO) {
        // 插入
        UserLikeDO userLike = BeanUtils.toBean(createReqVO, UserLikeDO.class);
        userLikeMapper.insert(userLike);
        // 返回
        return userLike.getId();
    }

    @Override
    public void updateUserLike(UserLikeSaveReqVO updateReqVO) {
        // 校验存在
        validateUserLikeExists(updateReqVO.getId());
        // 更新
        UserLikeDO updateObj = BeanUtils.toBean(updateReqVO, UserLikeDO.class);
        userLikeMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserLike(Long id) {
        // 校验存在
        validateUserLikeExists(id);
        // 删除
        userLikeMapper.deleteById(id);
    }

    private void validateUserLikeExists(Long id) {
        if (userLikeMapper.selectById(id) == null) {
            throw exception(USER_LIKE_NOT_EXISTS);
        }
    }

    @Override
    public UserLikeDO getUserLike(Long id) {
        return userLikeMapper.selectById(id);
    }

    @Override
    public PageResult<UserLikeDO> getUserLikePage(UserLikePageReqVO pageReqVO) {
        return userLikeMapper.selectPage(pageReqVO);
    }

}